/*
 * AsyncLogging.h
 *
 *  Created on: 2021年4月5日
 *      Author: san
 */

#ifndef BASE_ASYNCLOGGING_H_
#define BASE_ASYNCLOGGING_H_

#include "noncopyable.h"
#include "LogStream.h"
#include <vector>
#include <memory>
#include "Thread.h"


namespace san {

class AsyncLogging {
public:
	AsyncLogging(const std::string &basename, int flushInterval = 2);
	~AsyncLogging()
	{
		if (running_)
			stop();
	}

	void append(const char *logline, int len);

	void start()
	{
		running_ = true;
		thread_.start();
		latch_.wait();
	}

	void stop()
	{
		running_ = false;
		cond_.notify();
		thread_.join();
	}

private:
	void threadFunc();
	typedef FixedBuffer<kLargeBuffer> Buffer;
	typedef std::vector<std::shared_ptr<Buffer>> BufferVector;
	typedef std::shared_ptr<Buffer> BufferPtr;
	const int flushInterval_;
	bool running_;
	std::string basename_;
	Thread thread_;
	MutexLock mutex_;
	Condition cond_;
	BufferPtr currentBuffer_;
	BufferPtr nextBuffer_;
	BufferVector buffers_;
	CountDownLatch latch_;
};

} /* namespace san */

#endif /* BASE_ASYNCLOGGING_H_ */
